/**
 * 基础工具类
 * Created by 天上云好白 on 2017/1/4.
 */
var util = {
  Cookie: {
    /**
     * 设置cookie，默认储存时间为7天
     * @param key string
     * @param value string
     * @param time number
     */
    set: function (key, value, time) {
      var Days = time || 7
      var exp = new Date()
      exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000)
      document.cookie = key + '=' + value + ';expires=' + exp.toGMTString()
    },

    /**
     * 获取cookie
     * @param key string
     * @returns value || null
     */
    get: function (key) {
      var arr
      var reg = new RegExp('(^| )' + key + '=([^;]*)(;|$)')
      arr = document.cookie.match(reg)
      if (arr) {
        return arr[2]
      } else {
        return null
      }
    },

    /**
     * 删除cookie
     * @param key string
     */
    del: function (key) {
      var exp = new Date()
      exp.setTime(exp.getTime() - 1)
      var cval = this.getCookie(key)
      if (cval != null) {
        document.cookie = key + '=' + cval + ';expires=' + exp.toGMTString()
      }
    }
  }
}

export default util
